package com.wejoy.video.middle.station.admin.controller.verifycode;


import com.alibaba.fastjson2.JSONObject;
import com.wejoy.video.middle.station.data.service.common.IVerifyCodeService;
import com.wejoy.video.middle.station.data.util.ValidCodeUtil;
import com.wejoy.video.middle.station.data.util.baiduevs.EvsApiHttpUtils;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.vo.system.VerifyCodeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author: admin
 * @time: 2022/9/16 14:35
 */
@RestController
@RequestMapping("/login")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;
    /**
     * 验证码key开头
     */
    public final static String KEY_TOP = "verifyCode_";

    /**
     * 生成验证码图片与存入SESSION
     *
     * @param response
     * @param webSession
     * @return
     * @throws IOException
     */
    @ResponseBody
    @GetMapping("/getVerifyCode")
    public void getVerifyCode(HttpServletResponse response, HttpSession webSession) throws IOException {
        VerifyCodeVO verifyCode = verifyCodeService.generate(80, 28);
        String code = verifyCode.getCode();
        webSession.setAttribute(KEY_TOP + code, verifyCode);
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        ServletOutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException("获得响应流失败");
        }
        try {
            BufferedImage image = ValidCodeUtil.generateImg(code);
            ImageIO.write(image, "png", outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException("验证码输出到流失败");
        }
    }

    /**
     * 生成验证码图片与存入SESSION
     *
     * @param response
     * @param webSession
     * @return
     * @throws IOException
     */
    @ResponseBody
    @GetMapping("/verifyCode")
    public RespJSON verifyCode(HttpServletResponse response, HttpSession webSession) throws IOException {
        JSONObject verificationCode = EvsApiHttpUtils.getVerificationCode();
        return RespJSON.success(verificationCode);
    }


    /**
     * 生成验证码图片与存入SESSION
     *
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/verificationCode/{timestamp}")
    public void verificationCode(@PathVariable("timestamp") String timestamp, HttpServletResponse response) throws IOException {
        String code = verifyCodeService.saveCacheCodeByTimestamp(timestamp);
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        ServletOutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException("获得响应流失败");
        }
        try {
            BufferedImage image = ValidCodeUtil.generateImg(code);
            ImageIO.write(image, "png", outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException("验证码输出到流失败");
        }
    }
}